package com.hm.health.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hm.health.R;
import com.hm.health.entity.FileState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chenshi on 2020/1/14.
 */

public class FileStateAdapter extends RecyclerView.Adapter<FileStateAdapter.ViewHolder>{
    private Context mContext;
    private List<FileState> mDatas;

    public FileStateAdapter(Context context, List<FileState> datas) {
        mContext = context;
        mDatas = datas;
        if (null == mDatas) {
            mDatas = new ArrayList<>();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_raw_data_file_state,null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position >= mDatas.size()) {
            return;
        }

        holder.mTvFileName.setText(mDatas.get(position).getFileName());
        setState(holder.mTvFileName, mDatas.get(position).getFileState());
    }


    public void setState(TextView tvState, int fileState) {
        tvState.setTextColor(mContext.getResources().getColor(R.color.gray));
        switch (fileState) {
            case FileState.STATE_IDLE:
                tvState.setTextColor(mContext.getResources().getColor(R.color.blacktxtcolor));
                break;
            case FileState.STATE_EXECUTING:
                tvState.setTextColor(mContext.getResources().getColor(R.color.alarmtime_color));
                break;
            case FileState.STATE_FINISHED:
                tvState.setTextColor(mContext.getResources().getColor(R.color.green));
                break;
            case FileState.STATE_ERROR:
                tvState.setTextColor(mContext.getResources().getColor(R.color.red));
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_file_name)
        TextView mTvFileName;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
